
package com.rt.schedulenew.utils.util;
import org.slf4j.LoggerFactory;
import java.io.FileNotFoundException;
import java.io.FileInputStream;
import java.util.UUID;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTPFile;
import java.io.FileOutputStream;
import java.io.File;
import java.io.IOException;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTPClient;
import java.io.InputStream;
import org.slf4j.Logger;
public class FTPUtil
{
    private static final Logger log;
    
    public static boolean uploadFile(final String url, final int port, final String username, final String password, final String path, final String filename, final InputStream input) {
        boolean success = false;
        final FTPClient ftp = new FTPClient();
        try {
            ftp.connect(url, port);
            ftp.login(username, password);
            final int reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                FTPUtil.log.error("FTP登陆异常，错误码={}", (Object)reply);
                return success;
            }
            ftp.changeWorkingDirectory(path);
            ftp.storeFile(filename, input);
            input.close();
            ftp.logout();
            success = true;
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                }
                catch (IOException ex) {}
            }
        }
        return success;
    }
    
    public static boolean downFile(final String url, final int port, final String username, final String password, final String remotePath, final String fileName, final String localPath) {
        boolean success = false;
        final FTPClient ftp = new FTPClient();
        try {
            ftp.connect(url, port);
            ftp.login(username, password);
            final int reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                FTPUtil.log.error("FTP登陆异常，错误码={}", (Object)reply);
                return success;
            }
            ftp.changeWorkingDirectory(remotePath);
            final FTPFile[] listFiles;
            final FTPFile[] fs = listFiles = ftp.listFiles();
            for (final FTPFile ff : listFiles) {
                if (ff.getName().equals(fileName)) {
                    final File localFile = new File(localPath + "/" + ff.getName());
                    final OutputStream is = new FileOutputStream(localFile);
                    ftp.retrieveFile(ff.getName(), is);
                    is.close();
                }
            }
            ftp.logout();
            success = true;
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                }
                catch (IOException ex) {}
            }
        }
        return success;
    }
    
    public static boolean uploadFileFix(final String host, final int port, final String username, final String password, final String uploadPath, final String filename, final InputStream input) {
        boolean result = false;
        final FTPClient ftp = new FTPClient();
        try {
            ftp.connect(host, port);
            ftp.login(username, password);
            final int reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                return result;
            }
            String localCharset = "GBK";
            if (FTPReply.isPositiveCompletion(ftp.sendCommand("OPTS UTF8", "ON"))) {
                localCharset = "UTF-8";
            }
            ftp.setControlEncoding(localCharset);
            ftp.enterLocalPassiveMode();
            if (!ftp.changeWorkingDirectory(uploadPath)) {
                final String[] dirs = uploadPath.split("/");
                String tempPath = "";
                for (final String dir : dirs) {
                    if (null != dir) {
                        if (!"".equals(dir)) {
                            tempPath = tempPath + "/" + dir;
                            if (!ftp.changeWorkingDirectory(tempPath)) {
                                if (!ftp.makeDirectory(tempPath)) {
                                    return result;
                                }
                                ftp.changeWorkingDirectory(tempPath);
                            }
                        }
                    }
                }
            }
            ftp.setFileType(2);
            if (!ftp.storeFile(filename, input)) {
                return result;
            }
            input.close();
            ftp.logout();
            result = true;
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                }
                catch (IOException ex) {}
            }
        }
        return result;
    }
    
    public static byte[] downFileByte(final String fileName, final String url, final int port, final String username, final String password, final String remotePath) {
        byte[] return_arraybyte = null;
        final FTPClient ftpClient = new FTPClient();
        try {
            ftpClient.connect(url, port);
            ftpClient.login(username, password);
            ftpClient.changeWorkingDirectory(remotePath);
            final int reply = ftpClient.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftpClient.disconnect();
                FTPUtil.log.error("FTP登陆异常，错误码={}", (Object)reply);
                return null;
            }
            String localCharset = "GBK";
            if (FTPReply.isPositiveCompletion(ftpClient.sendCommand("OPTS UTF8", "ON"))) {
                localCharset = "UTF-8";
            }
            ftpClient.setControlEncoding(localCharset);
            ftpClient.enterLocalPassiveMode();
            final FTPFile[] listFiles;
            final FTPFile[] files = listFiles = ftpClient.listFiles();
            for (final FTPFile file : listFiles) {
                if (file.getName().equals(fileName)) {
                    ftpClient.enterLocalPassiveMode();
                    final InputStream ins = ftpClient.retrieveFileStream(file.getName());
                    final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
                    final byte[] buf = new byte[204800];
                    int bufsize = 0;
                    while ((bufsize = ins.read(buf, 0, buf.length)) != -1) {
                        byteOut.write(buf, 0, bufsize);
                    }
                    return_arraybyte = byteOut.toByteArray();
                    byteOut.close();
                    ins.close();
                    break;
                }
            }
            ftpClient.logout();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            if (ftpClient.isConnected()) {
                try {
                    ftpClient.disconnect();
                }
                catch (IOException ex) {}
            }
        }
        return return_arraybyte;
    }
    
    public static String fileNameConvert(final String oldName) {
        final String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        if (null == oldName || "".equals(oldName)) {
            return uuid;
        }
        return uuid + oldName.substring(oldName.lastIndexOf("."));
    }
    
    public static void main(final String[] args) throws FileNotFoundException {
        final File file = new File("D:\\111.pdf");
        for (int i = 0; i < 3; ++i) {
            final Boolean success = uploadFileFix("192.168.0.116", 21, "pacswrite", "pacswrite2002", "Image\\COMMON\\CT\\20200102\\CT00070308_1000000809", file.getName(), new FileInputStream(file));
            System.out.println(success);
        }
    }
    
    static {
        log = LoggerFactory.getLogger((Class)FTPUtil.class);
    }
}
